import javax.swing.*;
import java.beans.*;
import java.util.Observer;
import java.util.Observable;
import java.sql.*; 

public class Login extends Observable {
	
//vista    
JFrame ventanaLogin;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;

//instanciar modelos

private M_Configuraciones configuracion;


public Login(){
	iniciaVentana();
}


//Vista
public void iniciaVentana(){ 
 ventanaLogin =new JFrame();
   jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        ventanaLogin.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        ventanaLogin.setResizable(false);
        ventanaLogin.setTitle("Sistema de administraci\u00f3n de materiales y prestamos - Principal");
        jLabel1.setFont(new java.awt.Font("Arial", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Colegio Santa Maria de Maipu");

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Sistema de administracion del inventario de biblioteca");

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Ingreso al sistema");

        jLabel4.setText("Por favor ingrese su nombre y clave de acceso: ");
		
		ImageIcon icon1 = new ImageIcon("images/button_ok.png");
        jButton1.setIcon(icon1);
        jButton1.setText("Ingresar");

		jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
		ImageIcon icon2 = new ImageIcon("images/exit.png");
        jButton2.setIcon(icon2);
        jButton2.setText("Salir");
        
         jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jLabel5.setText("Nombre de usuario");

        jLabel6.setText("Contrase\u00f1a");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(ventanaLogin.getContentPane());
        ventanaLogin.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .add(jButton2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 494, Short.MAX_VALUE)
                                .add(jButton1))
                            .add(jLabel4)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel5)
                            .add(jLabel6))
                        .add(22, 22, 22)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(jTextField1)
                            .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 139, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(183, 183, 183))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .add(34, 34, 34)
                .add(jLabel3)
                .add(75, 75, 75)
                .add(jLabel4)
                .add(62, 62, 62)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel5)
                        .add(23, 23, 23)
                        .add(jLabel6)
                        .add(4, 4, 4))
                    .add(layout.createSequentialGroup()
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jPasswordField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(125, 125, 125)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton1))
                .add(23, 23, 23))
        );
        ventanaLogin.pack();
        ventanaLogin.setVisible(true);
        System.out.println("lsla");
}

//Controlador
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {


System.exit(0);
    }

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
Boolean login=false;
try{
	login= logear();
}catch(Exception e){}


if(login){
this.setChanged();
this.notifyObservers(); 
ventanaLogin.dispose();
 }else{
 	JOptionPane.showMessageDialog(ventanaLogin,
    	"El nombre de usuario y/o clave ingresados no son correctos.\nNo puede ingresar al sistema.",
    	"Aviso",
    	JOptionPane.INFORMATION_MESSAGE);
 }       
    }



private boolean logear()throws Exception{
configuracion = new M_Configuraciones();
return configuracion.ingresar(jTextField1.getText(), jPasswordField1.getText());


}

/*
public static void main(String args[]){
	Login i=new Login();
}
*/

}